package com.knowledge.plus.比较器.外部比较器.示例;

import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 实体类
 *
 * @author PF.Tian
 * @since 2022/3/1
 */
@Getter
@ToString
public class Record {
    //状态
    private String state;
    //时长
    private String time;

    public Record(String state, String time) {
        this.state = state;
        this.time = time;
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        Record record1 = new Record("通话中", "00:01:08");
        Record record2 = new Record("空闲", "00:18:02");
        Record record3 = new Record("通话中", "00:04:04");
        Record record4 = new Record("空闲", "00:01:57");
        List<Record> recordList = new ArrayList<>();
        recordList.add(record1);
        recordList.add(record2);
        recordList.add(record3);
        recordList.add(record4);
        System.out.println("排序前：" + recordList);
        /**
         * 写法一：在RecordComparator类（外部）实现比较
         */
        Collections.sort(recordList, new RecordComparator());

        /**
         * 写法二：在匿名内部类中实现比较
         */
        Collections.sort(recordList, new RecordComparator() {
            @Override
            public int compare(Record o1, Record o2) {
                final int stateCompare = o1.getState().compareTo(o2.getState());
                if (stateCompare == 0) {
                    return o1.getTime().compareTo(o2.getTime());
                }
                return stateCompare;
            }
        });
        System.out.println("排序后【时间正序】：" + recordList);


        recordList = new ArrayList<>();
        recordList.add(record1);
        recordList.add(record2);
        recordList.add(record3);
        recordList.add(record4);
        System.out.println("排序前：" + recordList);
        Collections.sort(recordList, new RecordTimeDescComparator());
        System.out.println("排序后【时间倒序】：" + recordList);

        System.out.println("--------------------------------------");

        /**
         * 写法三：在匿名内部类中实现比较
         */
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(-1);
        arrayList.add(3);
        arrayList.add(3);
        arrayList.add(-5);
        arrayList.add(7);
        arrayList.add(4);
        arrayList.add(-9);
        arrayList.add(-7);
        System.out.println("原始数组:");
        System.out.println(arrayList);
        // void reverse(List list)：反转
        Collections.reverse(arrayList);
        System.out.println("Collections.reverse(arrayList):");
        System.out.println(arrayList);

        // void sort(List list),按自然排序的升序排序
        Collections.sort(arrayList);
        System.out.println("Collections.sort(arrayList):");
        System.out.println(arrayList);
        /** 定制排序的用法 */
        Collections.sort(arrayList, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        System.out.println("定制排序后：");
        System.out.println(arrayList);
    }

}

